import xml.dom.minidom

class NirvanixException(Exception):
	def __init__(self, errormessage, code):
		self.errormessage = errormessage
		self.code = int(code)
	
	def __str__(self):
		return "Error Code: " + str(self.code) + " \r\nError Message: " + self.errormessage

def check_response(response_xml):
	doc = xml.dom.minidom.parseString(response_xml)
	response_code = doc.getElementsByTagName("ResponseCode")[0].childNodes[0].data
	# Check for any errors and raise an exception if there is a problem.
	if response_code != "0":
		raise NirvanixException(str(doc.getElementsByTagName("ErrorMessage")[0].childNodes[0].data),
			response_code)
	else:
		return doc